package com.j.lemon.learn.basic.file;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * StandardOpenOption; 与newBufferedWriter、newInputStream、newOutputStream、write一起使用
 *
 */
public class Tests {
    @Test
    public void test1() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("/Users/lijunjun/haha.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("/Users/lijunjun/haha.txt",true);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        //write
        bufferedOutputStream.write("eee".getBytes());
        bufferedOutputStream.flush();
        int len=0;
        byte b[] = new byte[1024];
        while ((len=bufferedInputStream.read(b))>0){
            System.out.println(new String(b,0,len));
        }


    }


    @Test
    void path(){
        Path path = Paths.get("/Users", "lijunjun", "work");
        Path test = path.resolve(Paths.get("test"));
    }


    @Test
    void file() throws IOException {
        Path dirPath = Paths.get("/Users", "lijunjun", "work");

        //创建新目录
        Files.createDirectory(dirPath);

        Path filePath = Paths.get("/Users", "lijunjun", "work","test.log");
        Path newFilePath = Paths.get("/Users", "lijunjun","test.log");

        //创建文件
        Files.createFile(filePath);

        //复制文件
        Files.copy(filePath,newFilePath, StandardCopyOption.REPLACE_EXISTING,StandardCopyOption.COPY_ATTRIBUTES);

        //复制流到文件
        Files.copy(new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }
        },filePath);

        //复制文件到流
        Files.copy(filePath, new OutputStream() {
            @Override
            public void write(int b) throws IOException {

            }
        });

        //移动文件
        Files.move(filePath,newFilePath,StandardCopyOption.ATOMIC_MOVE);

        //删除文件
        Files.delete(filePath);
        Files.deleteIfExists(filePath);







    }
}
